﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）
#include"Date.h"


///*构造函数*/Date::Date(int year , int month , int day )
//{
//	if (year >= 0 && month >= 1 && month <= 12 && day >= 1 && day <= GetMonthDay(year, month))
//	{
//		_year = year; _month = month; _day = day;
//	}
//	else cout << "date illegel" << endl;
//
//}


/*构造函数*/Date::Date(int year, int month, int day)
	:_year(year)
	, _month(month)
	, _day(day)
{
	if (year >= 0 && month >= 1 && month <= 12 && day >= 1 && day <= GetMonthDay(year, month))
	{
		_year = year; _month = month; _day = day;
	}
	else cout << "date illegel" << endl;
}
/*拷贝构造*/Date::Date(const Date& d)
{
	_year = d._year; _month = d._month; _day = d._day;
}
/*运算符重载*/
bool Date::operator<(const Date& d)const
{
	return (_year < d._year)
		|| (_year <= d._year && _month < d._month)
		|| (_year <= d._year && _month <= d._month && _day < d._day);
}
bool Date::operator>(const Date& d)const
{
	return (_year > d._year)
		|| (_year >= d._year && _month > d._month)
		|| (_year >= d._year && _month >= d._month && _day > d._day);
}
bool Date::operator==(const Date& d)const
{
	return !operator<(d) && !operator>(d);
}
bool Date::operator<=(const Date& d)const
{
	//老师的写法
	//return *this <d||*this==d;
	return !(operator>(d));//return operator==(d) || operator<(d);
}
bool Date::operator>=(const Date& d)const
{
	return !(operator<(d));
}
bool Date::operator!=(const Date& d)const
{
	return !operator==(d);
}

Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year; _month = d._month; _day = d._day;
	}
	return *this;
}
Date& Date::operator+=(int day)
{
	if (day < 0)return *this -= -day;
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;

		if (_month == 13)
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}
Date Date::operator+(int day)//循环
{
	Date ret(*this);
	ret += day;//复用+=
	return ret;

}
Date& Date::operator-=(int day)
{

	if (day < 0)return *this += -day;
	_day -= day;
	while (_day < 1)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;//复用-=
	return ret;
}
Date& Date::operator++()//前置返回加之后的值
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)//后置，返回加之前值 
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
int Date::operator-(const Date& d)const//老师的
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d) { max = d; min = *this; flag = -1; }
	int n = 0;
	while (min != max)
	{
		++n;
		++min;
	}
	return n * flag;
}

/*取地址重载*/Date* Date::operator&()
{
	return this;
}
/*const取地址重载*/const Date* Date::operator&()const
{
	return this;
}

/*打印日期*/void Date::Print()const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
/*根据年月获取当月天数*/int Date::GetMonthDay(int year, int month)const
{
	static int monthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		return 29;
	}
	return monthDays[month];
}

/*析构函数*/Date::~Date()
{
	_year = 0; _month = _day = 1;
}